/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.method
 */
package com.apache.license.filter;

import java.text.SimpleDateFormat;

import javax.servlet.Filter;
import javax.servlet.ServletException;

import com.apache.tools.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ClassUtils;

import com.apache.license.validator.LicenseValidate;

/**
 * description:  启用license父类filter
 * @author Hou Dayu 创建时间：2016-12-28
 * @Copyright(c)2014:
 */
public abstract class AbstractFilter implements Filter {

	private boolean isUseLicense = false;
	private Logger log = LoggerFactory.getLogger("License Log");

	//license文件初始化
	protected void initLincense() throws ServletException {
		String path = "";
		if (LicenseValidate.getInstance().isInitMark()) {//初化后不在初始化
			return;
		}
		try {
			path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
		} catch (NullPointerException e) {
			path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
		}
		path = path.substring(1);
		String publicpath = path + "/config/keyPair_public.cer";
		String privatepath = path + "/config/keyPair.cer";
		try {
			initLicense("", publicpath, privatepath);
		} catch (Exception e) {
			try {
				initLicense("jar", "/config/keyPair_public.cer", "/config/keyPair.cer");
			} catch (Exception e1) {
				log.debug("产品还没有被授权或License 超过有效期！");
				System.exit(0);
			}
		}
	}

	private void initLicense(String path, String publicpath, String privatepath) throws Exception {
		if (!isUseLicense) {
			if ("".equals(path))
				isUseLicense = LicenseValidate.getInstance().validate(publicpath, privatepath);
			else
				isUseLicense = LicenseValidate.getInstance().validate(path, publicpath, privatepath);
			if (!isUseLicense) {
				log.debug("无效的产品License！");
				System.exit(0);
			} else {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				if ("9".equals(LicenseValidate.getInstance().getMsg().getIsformal())) {
					printPlateFormIco();
					System.out.println("  ( SourceForge ) \n");
				} else {
					if (LicenseValidate.getInstance().islasttime()) {
						log.debug("您的License起始日期为："
								+ format.format(LicenseValidate.getInstance().getMsg().getBegintime()));
						log.debug("有效期为：" + LicenseValidate.getInstance().getMsg().getTimeleng() + "个月");
						log.debug("授权公司：" + LicenseValidate.getInstance().getMsg().getCompany());
						printPlateFormIco();
					} else {
						log.info("您的License起始日期为："
								+ format.format(LicenseValidate.getInstance().getMsg().getBegintime()));
						log.info("有效期为：" + LicenseValidate.getInstance().getMsg().getTimeleng() + "个月");
						log.info("License已超过有效期！");
						System.exit(0);
					}
				}
			}
		}
	}

	private void printPlateFormIco() {
		Package packageObj= this.getClass().getPackage();
		String versionStr = "";
		String version_implement = packageObj.getImplementationVersion();
		String version_specificat = packageObj.getSpecificationVersion();
		versionStr = StrUtil.doNull(version_implement,StrUtil.doNull(version_specificat,"Development Alpha"));
		System.out.println("    _    _    _    ______");
		System.out.println("   (*)  | |  | |  |  ____|");
		System.out.println("   | |  | |  | |  | |____");
		System.out.println("   | |  | |  | |  |____  |");
		System.out.println("   | |  | |__| |   ____| |");
		System.out.println("   |_|  \\______/  |______|");
		System.out.println("");
		System.out.println("  :: www.iussoft.com :: ( v-"+versionStr+" )");
		System.out.println("");
	}
}
